/**
 * @file temperature.c
 * @brief 
 * @author 2bam 
 * @version 1.0
 * @date 2023-09-02
 * @copyright Copyright (c) 2023  NULL
 * 
 * @change log:
 * Date           Version Author   Describe
 * 2023-09-02     1.0     2bam     first version
 */

/* Includes -------------------------------------------------------------------*/
#include <rtthread.h>
#include <rtdevice.h>
#include "stdbool.h"
#include <stdlib.h>
#include <stddef.h>
#include <drv_common.h>

#define DBG_TAG "temperature"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
/* Private define -------------------------------------------------------------*/
#define ON_CHIP_TEMPERATURE_ADC_CHANNEL_NUM    16
#define ADC1_PA4_CHANNEL_NUM    4
/* Private typedef ------------------------------------------------------------*/

/* Private variables ----------------------------------------------------------*/
rt_adc_device_t adc_dev = RT_NULL;
static rt_thread_t adc_thread = RT_NULL;          //线程句柄
/* Private function prototypes ------------------------------------------------*/


/**
 * @brief 
 * @param  argc        My Param doc
 * @date 2023-09-02
 * @author 2bam 
 */
void adc_task_entry(void *argc)
{
    rt_uint32_t val = 0;
    while (1)
    {
        val = rt_adc_read(adc_dev,ON_CHIP_TEMPERATURE_ADC_CHANNEL_NUM);
        //rt_kprintf("val:%d\n",val);
        rt_thread_mdelay(1000);
    }
    
}

/**
 * @brief 
 * @return int 
 * @date 2023-09-02
 * @author 2bam 
 */
int adc1_init(void)
{
    extern void MX_ADC1_Init(void);
    MX_ADC1_Init();

    rt_err_t ret = 0;
    adc_dev = (rt_adc_device_t)rt_device_find("adc1");
    if(adc_dev == RT_NULL)
    {
        LOG_E("find adc1 failed!\n");
    }
    else
    {
        LOG_D("find adc1 success.\n");
    }

    ret = rt_adc_enable(adc_dev,ON_CHIP_TEMPERATURE_ADC_CHANNEL_NUM);
    if(ret != RT_EOK)
    {
        LOG_E("adc enable failed!\n");
    }



    adc_thread = rt_thread_create("adc_task",adc_task_entry,RT_NULL,1024,8,100);
    if(adc_thread == RT_NULL)
    {
        LOG_E("adc_task create failed!\n");
    }
    else
    {
        LOG_D("adc_task create success.\n");
    }
    /* 启动线程 */
    rt_thread_startup(adc_thread);

    return 0;
}
INIT_APP_EXPORT(adc1_init);
